﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace RhythmGame
{

    internal class BeatMap
    {
        private string songId;
        private string beatmapId;
        private string name;
        private string artist;
        private string mapper;
        private TimeSpan time;
        private int difficulty;
        private int level;
        private string audioPath;
        private string coverPath;
        private string backgroundPath;
        private string videoPath;

        private string path;
        private List<Element_info> elements = new List<Element_info>();
        private int preTime;
        public BeatMap() { }
        public BeatMap(string songId, string beatmapId, string name, string artist, string mapper,
                        TimeSpan time, int difficulty, int level, string audioPath, string coverPath,
                        string backgroundPath, string videoPath, string path)
        {
            this.InitBeatMap(songId, beatmapId, name, artist, mapper, time, difficulty, level, audioPath, coverPath, backgroundPath, videoPath, path);
        }

        public void InitBeatMap(string songId, string beatmapId, string name, string artist, string mapper,
                        TimeSpan time, int difficulty, int level, string audioPath, string coverPath,
                        string backgroundPath, string videoPath, string path)
        {
            this.songId = songId;
            this.beatmapId = beatmapId;
            this.name = name;
            this.artist = artist;
            this.mapper = mapper;
            this.time = time;
            this.difficulty = difficulty;
            this.level = level;
            this.audioPath = audioPath;
            this.coverPath = coverPath;
            this.backgroundPath = backgroundPath;
            this.videoPath = videoPath;
            this.path = path;

            this.preTime = (int)(Settings.Instance.JudgeHeight /
                (Settings.Instance.DropSpeed));//TODO �����¼������ı�
                                             // MessageBox.Show("Config.Instance.WindowsHeight: " + Config.Instance.WindowsHeight);
        }
        public string SongId => songId;
        public string BeatmapId => beatmapId;
        public string Name => name;
        public string Artist => artist;
        public string Mapper => mapper;
        public TimeSpan Time => time;
        public int Difficulty => difficulty;
        public int Level => level;
        public string AudioPath => audioPath;
        public string CoverPath => coverPath;
        public string BackgroundPath => backgroundPath;
        public string VideoPath => videoPath;

        public string Path => path;
        public void AddElement(Element_info e)
        {
            elements.Add(e);
        }
        public void AddElements(Element_info[] e)
        {
            elements.AddRange(e);
        }
        public void RemoveElement(Element_info e)
        {
            elements.Remove(e);
        }
        // return the new elements in this tick
        public Element_info[] GetNextTickInfo(int lastTime, int currentTime)
        {
            // find the elements that should be shown in this tick
            Element_info[] newElements = elements.Where(e => e.ClickTime - this.preTime >= lastTime && e.ClickTime - this.preTime < currentTime).ToArray();
            foreach (Element_info e in newElements)
            {
                e.ShowTime = e.ClickTime - this.preTime;
            }
            // remove the elements that should be shown in this tick
            elements.RemoveAll(e => e.ClickTime < currentTime);
            return newElements;
        }

        // check if the cover audio video and background can be loaded
        public bool CheckResources()
        {
            return true;
        }

        public bool Finish()
        {
            return elements.Count == 0 && CheckResources();
        }

        public override string ToString()
        {
            return "SongId: " + songId + "\n" +
                "BeatmapId: " + beatmapId + "\n" +
                "Name: " + name + "\n" +
                "Artist: " + artist + "\n" +
                "Mapper: " + mapper + "\n" +
                "Time: " + time + "\n" +
                "Difficulty: " + difficulty + "\n" +
                "Level: " + level + "\n" +
                "AudioPath: " + audioPath + "\n" +
                "CoverPath: " + coverPath + "\n" +
                "BackgroundPath: " + backgroundPath + "\n" +
                "VideoPath: " + videoPath + "\n" +
                "Elements: " + elements.Count;

        }

        public Element_info[] GetElements()
        {
            return elements.ToArray();
        }

        public void UpdatePreTime()
        {
            this.preTime = (int)(Settings.Instance.JudgeHeight /
                (Settings.Instance.DropSpeed * Settings.Instance.FPS / 120 / (double)Settings.Instance.WinHeight));//TODO �����¼������ı�
        }

        public int GetTotalNotes()
        {
            return elements.Count;
        }

        public bool HasBreakNotes()
        {
            return elements.Any(e => e.TypeId == 2 || e.TypeId == 4/* Break 物块的 TypeId，例如 2 */);
        }

        public int GetTotalBreaks()
        {
            return elements.Count(e => e.TypeId == 2 || e.TypeId == 4/* Break 物块的 TypeId，例如 2 */);
        }
    }
}
